'''
Created on Apr 19, 2009

@author: Administrator
'''
from django.utils.translation import ugettext_lazy as _
from com.ericyang.groupshopping.addressbook.models import  AddressBook
from django import forms
from com.ericyang.common.web.middleware import threadlocals

class AddressBookCreationForm(forms.ModelForm):
    '''
    form of address book creation
    '''
    name = forms.CharField(label=_("name"), max_length=50)
    phone = forms.CharField(label=_("phone"), max_length=30)
    email = forms.EmailField(label=_("email"), max_length=75)
    address = forms.CharField(label=_("address"), max_length=200)
    postnum = forms.CharField(label=_("postnum"), max_length=20)
    province = forms.CharField(label=_("province"), max_length=30)
    city = forms.CharField(label=_("city"), max_length=30)
    district = forms.CharField(label=_("district"), max_length=100)
    
    class Meta:
         model = AddressBook
         fields = ("name", 'phone', 'email', 'address', 'postnum', 'province', 'city', 'district')

    def save(self, commit=True):
        addressbook = super(AddressBookCreationForm, self).save(commit=False)
        addressbook.user = threadlocals.get_current_user()

        if commit:
            addressbook.save()
        return addressbook

class AddressBookChangeForm(forms.ModelForm):
    '''
    form of address book change
    '''
    name = forms.CharField(label=_("name"), max_length=50)
    phone = forms.CharField(label=_("phone"), max_length=30)
    email = forms.EmailField(label=_("email"), max_length=75)
    address = forms.CharField(label=_("address"), max_length=200)
    postnum = forms.CharField(label=_("postnum"), max_length=20)
    province = forms.CharField(label=_("province"), max_length=30)
    city = forms.CharField(label=_("city"), max_length=30)
    district = forms.CharField(label=_("district"), max_length=100)
    
    class Meta:
         model = AddressBook
         fields = ("name", 'phone', 'email', 'address', 'postnum', 'province', 'city', 'district')
         
    def __init__(self, addressbook, *args, **kwargs):
        self.addressbook = addressbook
        super(AddressBookChangeForm, self).__init__(*args, **kwargs)

    def _data(self, name):
        return self.cleaned_data[name]

    def save(self, commit=True):
#        self.addressbook.name = self.cleaned_data['name']
#        self.addressbook.phone = self.cleaned_data['phone']
#        self.addressbook.email = self.cleaned_data['email']
#        self.addressbook.address = self.cleaned_data['address']
#        self.addressbook.postnum = self.cleaned_data['postnum']
#        self.addressbook.province = self.cleaned_data['province']
#        self.addressbook.city = self.cleaned_data['city']
#        self.addressbook.district = self.cleaned_data['district']
        if commit:
             self.addressbook.save()
        return  self.addressbook
    